#!/usr/bin/python
#encoding:utf-8
import urllib
import urllib2
import datetime
from bs4 import BeautifulSoup
from mailqq import *

test_data = {'type':'news','date':'2015-02-27'}
test_data['date'] = (datetime.datetime.now()-datetime.timedelta(days = 1)).strftime("%Y-%m-%d")
test_data_urlencode = urllib.urlencode(test_data)

requrl = "http://www.dailyfx.com.hk/inc/process.php"

req = urllib2.Request(url = requrl,data =test_data_urlencode)

res_data = urllib2.urlopen(req)
content = res_data.read()
ucontent = unicode(content,"utf-8")
soup = BeautifulSoup(ucontent)
mail_content = u""
for news in soup.findAll('tr', class_="record"):
	 news_date = news.contents[0]
	 news_content = news.contents[1]
	 if news_content.find('a') != None:
		news_contents = news_content.find('a').contents[0].strip()
		news_contents += news_content.find('div').contents[0].strip()
		if news_content.find('div').find('a') != None:
		    news_contents += news_content.find('div').find('a').contents[0].strip()
	 else:
		news_contents = news_content.contents[0].strip()
	 mail_content += news_date.contents[0].strip()
	 mail_content += u" "
	 mail_content += news_contents
	 mail_content += u"<br/>"
if mail_content != u"":
    mail_to_qq("411486203@qq.com", "daily "+test_data['date'], mail_content)
else:
    mail_to_qq("411486203@qq.com", "daily "+test_data['date'], "没有新闻")

